package ru.urfc.portal.developer_tasks.ejb_tests;

import java.io.ByteArrayInputStream;
import java.io.IOException;

import javax.ejb.EJB;

import org.apache.commons.io.output.ByteArrayOutputStream;

import ru.urfc.portal.Utils;
import ru.urfc.portal.dao.AttachedFileDao;
import ru.urfc.portal.dao.filter.Filter;
import ru.urfc.portal.developer_tasks.BaseTestEjb;
import ru.urfc.portal.developer_tasks.Initializer;
import ru.urfc.portal.model.documents.AttachedFile;

public class AttachedFileDaoTestEjb extends BaseTestEjb {

	@EJB AttachedFileDao attachedFileDao;
	AttachedFile attachedFile;
	
	@Override
	protected void setUp() throws Exception {
		attachedFile = exactlyOne(find(AttachedFile.class, new Filter("name", Initializer.CLIENT_MTS_DOCUMENT_ATTACHMENT_FILE_NAME)));
	}
	
	String content = "qq1233";
	byte[] contentBytes = content.getBytes();
	
	public void testWriteAndRead() throws IOException {
		attachedFileDao.writeContent(attachedFile, new ByteArrayInputStream(contentBytes), 
				contentBytes.length);
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		Utils.copyStream(attachedFileDao.readContent(attachedFile), outputStream);
		assertEquals(content, new String(outputStream.toByteArray()));
	}

}
